//
//  YKTurnOverViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/15.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKTurnOverViewController.h"
#import "YKSelectView.h"
#import "YKPopupView.h"
#import "YKTurnOverUserModel.h"
#import "YKInputView.h"
#import "WorkFlowManager.h"

@interface YKTurnOverViewController ()
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIButton *submitButton;
@property (nonatomic, strong)YKSelectView *selectView;
@property (nonatomic, strong)UILabel *nextTaskLabel;

@end

@implementation YKTurnOverViewController

- (void)updateNextAssignees {
    
    self.selectView = [[YKSelectView alloc]initWithTitle:@"下一处理人" row:self.users initialText:self.users.firstObject weights:@[@1,@2]];
    [self.view addSubview:self.selectView];
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.nextTaskLabel.mas_bottom).offset(11);
    }];
    if (self.nextTask){
        self.nextTaskLabel.text = [NSString stringWithFormat:@"下一个任务：%@", self.nextTask.name];
    }
    else {
        self.nextTaskLabel.text = @"";
    }

    UITextView *textView = [[UITextView alloc]init];
    textView.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
    [self.view addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.selectView.mas_bottom).offset(11);
        make.bottom.equalTo(self.submitButton.mas_top).offset(-11);
    }];
    self.textView = textView;
    [self.view layoutIfNeeded];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"审批意见";
    titleLabel.textColor = [UIColor tf_colorWithHexString:@"#444444"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(11);
    }];
    
    self.titleLabel = titleLabel;
    
    self.nextTaskLabel = [[UILabel alloc]init];
    [self.view addSubview:self.nextTaskLabel];
    [self.nextTaskLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(8);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(8);
        make.trailing.equalTo(self.view.mas_trailing).offset(-8);
    }];
    
    UIButton *submitButton = [[UIButton alloc]init];
    self.submitButton = submitButton;
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
//    [submitButton setBackgroundColor:[UIColor tf_colorWithHexString:@"#00a0e8"]];
    [submitButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];
    [submitButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];
    [submitButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [self.view addSubview:submitButton];
    [submitButton addTarget:self action:@selector(onSubmitButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:14];
    submitButton.layer.cornerRadius = 5;
    
    
    
    if (self.m_id) {
        switch (self.type) {
            case YKTurnOverTypeTurnOver:
            case YKTurnOverTypeApprove:
            {
                [self tf_showStateView:kTFViewStateLoading];
                [WorkFlowManager getNextAssigneeWithBusinessID:self.m_id completion:^(NSArray<YKTurnOverUserModel *> *nextAssignees, YKTurnOverUserTaskModel *nextTask, NSError *error) {
                    if (!error){
                        self.users = nextAssignees;
                        self.nextTask = nextTask;
                        [self updateNextAssignees];
                        [self tf_showStateView:kTFViewStateNone];
                    }
                    else {
                        [self tf_showStateView:kTFViewStateNone];
                        [self showHint:error.localizedFailureReason];
                        [YKPopupView dismissViewController:self];
                    }
                }];
            }
                break;
            case YKTurnOverTypeDisapprove:
            {
                [self tf_showStateView:kTFViewStateLoading];

                [WorkFlowManager getPreTaskWithBusinessID:self.m_id completion:^(NSArray<YKTurnOverUserModel *> *nextAssignees, NSError *error) {
                    if (!error){
                        self.users = nextAssignees;
                        if (self.users.count){
                            self.selectView = [[YKSelectView alloc]initWithTitle:@"上一任务" row:self.users initialText:self.users.firstObject weights:@[@1,@2]];

                            [self.view addSubview:self.selectView];
                            [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
                                make.leading.trailing.equalTo(self.view);
                                make.top.equalTo(titleLabel.mas_bottom).offset(11);
                            }];

                            UITextView *textView = [[UITextView alloc]init];
                            textView.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
                            [self.view addSubview:textView];
                            [textView mas_makeConstraints:^(MASConstraintMaker *make) {
                                make.leading.trailing.equalTo(self.view);
                                make.top.equalTo(self.selectView.mas_bottom).offset(11);
                                make.bottom.equalTo(submitButton.mas_top).offset(-11);
                            }];
                            self.textView = textView;
                            [self.view layoutIfNeeded];
                        }
                        else {
                            [self showHint:@"没有找到上级任务"];
                            [YKPopupView dismissViewController:self];
                        }
                        [self tf_showStateView:kTFViewStateNone];
                    }
                    else {
                        [self tf_showStateView:kTFViewStateNone];
                        [self showHint:error.localizedFailureReason];
                        submitButton.enabled = NO;
                        [YKPopupView dismissViewController:self];
                    }
                }];

//                [TFHTTPMANAER POST:@"workExamine/preTask.htm" parameters:@{@"businessId":self.m_id} success:^(id responseObject) {
//                    self.users = [NSArray yy_modelArrayWithClass:[YKTurnOverUserModel class] json:[responseObject valueForKeyPath:@"userList.mapSel"]];
//                    self.selectView = [[YKSelectView alloc]initWithTitle:@"上一任务" row:self.users initialText:self.users.firstObject weights:@[@1,@2]];
//
//                    [self.view addSubview:self.selectView];
//                    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
//                        make.leading.trailing.equalTo(self.view);
//                        make.top.equalTo(titleLabel.mas_bottom).offset(11);
//                    }];
//
//                    UITextView *textView = [[UITextView alloc]init];
//                    textView.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
//                    [self.view addSubview:textView];
//                    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
//                        make.leading.trailing.equalTo(self.view);
//                        make.top.equalTo(self.selectView.mas_bottom).offset(11);
//                        make.bottom.equalTo(submitButton.mas_top).offset(-11);
//                    }];
//                    self.textView = textView;
//                    [self.view layoutIfNeeded];
//                    [self tf_showStateView:kTFViewStateNone];
//                } failure:^(id responseObject, NSError *error) {
//                    //[self tf_showStateView:kTFViewStateNetError];
//                    [self tf_showStateView:kTFViewStateNone];
//                    [self showHint:error.localizedFailureReason];
//                    submitButton.enabled = NO;
//                    [YKPopupView dismissViewController:self];
//                }];
            }
                break;
            default:
                break;
        }
    }
}

- (void)onSubmitButtonTapped:(id)sender {
    
    if (self.submitBlock) {
        if (self.type == 2) {
            self.submitBlock(self.textView.text, self.m_id, self.selectView.selectedRow.name);
        }
        else {
            self.submitBlock(self.textView.text, self.m_id, self.selectView.selectedRow.id_p);
        }
    }
    [YKPopupView dismissViewController:self];
}

@end
